import axios from "axios"
import toast from "vant/lib/toast/index.js"
import websize from "@/const/index.js"

const req = axios.create({
  baseURL: `${websize.BASEURL}/a/mobile/mobileTaskNew`
  // baseURL: '/taskManager/a/mobile/mobileTaskNew'
})

/*
 *请求前
 */
req.interceptors.request.use(
  (config) => {
    config.headers.token = localStorage.getItem("token")

    // post请求的时候，改变请求头的Content-Type方式
    // if (config.method === 'post') {
    //   config.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
    // }
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

/*
 *请求后
 */
req.interceptors.response.use((res) => {
  // 判断是否有返回码（1001开头都是token的错误）
  if (res.data.code) {
    if (String(res.data.code).indexOf("1001") === 0) {
      toast(res.data.msg)
    }
  }
  return res.data
})

export default req
